﻿Shader "Unlit/Wave1"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
	}
	
	CGINCLUDE
	#include "UnityCG.cginc"
	struct v2f
	{
	   half4 pos : SV_POSITION;
	   half2 uv :TEXCOORD0;
	   half4 uv01 : TEXCOORD1;
	   half4 uv23 : TEXCOORD2;
	   half4 uv45 : TEXCOORD3;
	   half4 uv67 : TEXCOORD4;
	};
	
	sampler2D _MainTex;
	half4 _MainTex_TexelSize;
	half4 _Offsets;
	half4 _WaveStrength;
	
	v2f vert(appdata_img v)
	{
	   v2f o;
	   o.pos = UnityObjectToClipPos(v.vertex);
	   o.uv.xy = v.texcoord.xy;
	   o.uv01 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1,-1,-1);
	   o.uv23 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1,-1,-1) * 2.0;
	   o.uv45 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1,-1,-1) * 3.0;
	   o.uv67 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1,-1,-1) * 4.0;
	   o.uv67 = v.texcoord.xyxy + _Offsets.xyxy * half4(1,1,-1,-1) * 5.0;

	   return o;
	}
	
	half4 frag(v2f i) : SV_Target
	{
	   half2 dv = half2(0.5,0.5) - i.uv;
	   dv = dv * half2(_ScreenParams.x/_ScreenParams.y,1);
	   half dis = sqrt(dv.x*dv.x + dv.y * dv.y);
	   
	   half _distanceFactor = 10.0/(_WaveStrength + 0.1);
	   half _totalFactor = 20.0;
	   half _waveWith = 0.25;
	   half _curWaveDis = _WaveStrength;
	   
	   half sinFactor = sin(dis * _distanceFactor) * _totalFactor * 0.01;
	   
	   half discardFactor = clamp(_waveWith - abs(_curWaveDis - dis),0,1);
	   
	   half2 dv1 = normalize(dv);
	   
	   half2 offset = dv1 * sinFactor * discardFactor;
	   
	   //
	   half4 color = half4(0,0,0,0);
	   color += 0.225 * tex2D(_MainTex,offset + i.uv);
	   color += 0.150 * tex2D(_MainTex,offset + i.uv01.xy);
	   color += 0.150 * tex2D(_MainTex,offset + i.uv01.zw);
	   
	   color += 0.110 * tex2D(_MainTex,offset + i.uv23.xy);
	   color += 0.110 * tex2D(_MainTex,offset + i.uv23.zw);
	   
	   color += 0.075 * tex2D(_MainTex,offset + i.uv45.xy);
	   color += 0.075 * tex2D(_MainTex,offset + i.uv45.zw);
	   
	   color += 0.0525 * tex2D(_MainTex,offset + i.uv67.xy);
	   color += 0.0525 * tex2D(_MainTex,offset + i.uv67.zw);
	   
	   return color;
	}
	
	ENDCG
	
	SubShader
	{
		Cull Off
		ZTest Off
		ZWrite Off

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			ENDCG
		}
	}
	Fallback off
}
